<template>
  <PageListContainer class="agent-list">
    <div class="base-block">
      <k-search :model="state.params" @search="methods.onSearch" @reset="onClickReset">
        <el-form-item label="代理搜索">
          <el-input v-model="state.params.keyword" clearable placeholder="请输入代理名称、代理账号" style="width: 250px" />
        </el-form-item>
        <el-form-item label="加入时间">
          <el-date-picker
            v-model="state.params._timeArr"
            type="daterange"
            format="YYYY-MM-DD"
            value-format="YYYYMMDD"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :shortcuts="dateShortcut"
            @change="timeChange"
          />
        </el-form-item>
      </k-search>
    </div>
    <div class="base-block">
      <KToolbar>
        <el-button type="primary" @click="onClickAdd">新增代理</el-button>
        <template #right>
          <el-button type="default" :icon="Download" @click="onClickExportData">导出数据</el-button>
        </template>
      </KToolbar>

      <KTable v-bind="tableState" v-on="tableEvent">
        <template #actions="s">
          <el-link type="primary" @click="onClickEdit(s.row)">编辑</el-link>
        </template>
      </KTable>
    </div>
    <!-- 新增编辑 -->
    <AddEditModal ref="addEditModal" @success="methods.onSearch" />
  </PageListContainer>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { Download } from '@element-plus/icons-vue';
import { useTableList } from './hooks/use-table';
import { colonelListExport } from '@/api/crm-kgj/download';
import AddEditModal from './components/add-edit-modal.vue';
import { toolGetDateRangeShorcuts as dateShortcut } from '@/utils';
import { AuthCode, hasPermission } from '@/modules/auth';
defineOptions({ name: 'AgencyAgentList' });
const { state, tableState, tableEvent, ...methods } = useTableList({});
// 重置
const onClickReset = () => {
  methods.onReset();
};
const onClickExportData = () => {
  if (hasPermission(AuthCode.AGENCY_AGENT_LIST)) {
    colonelListExport(methods.getParams(state.params).data);
  }
};
// 新增
const addEditModal = ref();
const onClickAdd = () => {
  if (hasPermission(AuthCode.AGENCY_AGENT_UPDATE)) {
    addEditModal.value.show(0);
  }
};
// 编辑
const onClickEdit = (row: any) => {
  if (hasPermission(AuthCode.AGENCY_AGENT_UPDATE)) {
    addEditModal.value.show(1, row);
  }
};
/**
 * @时间控件
 */

const timeChange = () => {
  methods.onSearch();
};
</script>
